package com.jack.redismq.consumer;

import com.jack.redismq.util.RedisObjectSerializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;

/**
 * Created by zhang_j on 2019/12/3
 */
public class RedisListener implements MessageListener {
    private static final Logger LOGGER = LoggerFactory.getLogger(RedisListener.class);

    @Override
    public void onMessage(Message message, byte[] pattern) {
        LOGGER.info("从消息通道={}监听到消息",new String(pattern));
        LOGGER.info("从消息通道={}监听到消息",new String(message.getChannel()));
        LOGGER.info("元消息={}",new String(message.getBody()));

        /**
         * 新建一个用于反序列化的对象，注意这里的对象和前面配置的一样
         * 因为我前面设置的默认序列化方式为GenericJackson2JsonRedisSerializer
         * 所以这里的实现方式为GenericJackson2JsonRedisSerializer
         */
//        RedisSerializer serializer = new GenericJackson2JsonRedisSerializer();
        RedisObjectSerializer redisObjectSerializer = new RedisObjectSerializer();
        LOGGER.info("反序列化后的消息={}",redisObjectSerializer.deserialize(message.getBody()));
    }
}
